package jpaperwork.web;


import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.Headers;

import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.Executors;

/**
 * User: fleipold
 * Date: May 4, 2009
 * Time: 11:52:08 AM
 */
public class JdkServer implements HttpHandler {
    public JdkServer() throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(9999),0);
        server.setExecutor(Executors.newFixedThreadPool(1));
        server.createContext("/", this);
        server.start();
        

    }

    public static void main(String[] args) throws IOException {
        new JdkServer();



    }

    public void handle(HttpExchange httpExchange) throws IOException {

        URI path = httpExchange.getRequestURI();
        
        Headers responseHeaders = httpExchange.getResponseHeaders();
        responseHeaders.set("Content-Type", "text/html");
        httpExchange.sendResponseHeaders(200, 0);


        Writer writer = new OutputStreamWriter(httpExchange.getResponseBody());
        writer.write("<html><body><h1>Hello World!</h1>"+path.toString()+"</body></html>");

        writer.close();
        httpExchange.getResponseCode();

        httpExchange.close();
        
    }
}
